package com.blog.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.blog.common.R;
import com.blog.domain.Article;
import com.blog.domain.Swipe;
import com.blog.service.IArticleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @AUTHOR:HZG MADE IN CHINA
 */

@RestController
@RequestMapping("/articles")
public class ArticleController {


    @Autowired
    private IArticleService articleService;


    /**
     * 新增article
     * @param article
     * @return
     */
    @PostMapping("/publish")
    public R<Article> save(HttpServletRequest request, @RequestBody Article article){
        Integer userId = (Integer) request.getSession().getAttribute("muserId");

         if (userId != null){
            article.setArtId(userId);
        }
        articleService.save(article);
        return R.success("发表成功");
    }


    /**
     * 根据id查询发表文章
     * @param userId
     * @return
     */
    @GetMapping("/{userId}")
    public R<List<Article>> getAll(@PathVariable Integer userId){

        List<Article> article = articleService.getByUserId(userId);
         if(article != null){
            return R.success(article,"查询成功");
        }
        return R.error("查询失败");
    }

    /**
     * 根据artId查询文章
     *
     * @param artId
     * @return
     */
    @GetMapping("/show/{artId}")
    public R<Article> getByArtId(@PathVariable Integer artId) {
        LambdaQueryWrapper<Article> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Article::getArtId, artId);
        Article mArticle = articleService.getOne(queryWrapper);
        if (mArticle == null) {
            return R.error("查询失败");
        }
        return R.success(mArticle, "查询成功");
    }

    /**
     * 修改article信息
     *
     * @param article
     * @return
     */

    @PutMapping("/modify")
    public R update(@RequestBody Article article) {

        //根据页面提交的artId查询数据库
        LambdaQueryWrapper<Article> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Article::getArtId, article.getArtId());
        Article mArticle = articleService.getOne(queryWrapper);


        if (mArticle == null) {
            return R.error("查询失败");
        }
        article.setUserId(mArticle.getUserId());
        articleService.updateById(article);

        return R.success(article, "修改成功");
    }
    /**
     * 根据artId删除article
     * @param artId
     * @return
     */
    @DeleteMapping("/delete/{artId}")
    public R<String> delete(@PathVariable Integer artId){



        //根据页面提交的artId查询数据库
        LambdaQueryWrapper<Article> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Article::getArtId,artId );
        Article mArticle = articleService.getOne(queryWrapper);


        if (mArticle == null) {
            return R.error("查询失败");
        }

        articleService.removeById(artId);

        return R.success("删除成功");
    }

}
